from users.models import EmailVerifyRecord
from django.core.mail import send_mail
import random
import string


def random_str(randomlength=8):
    """
    生成随机字符串
    :param randomlength: 字符串长度
    :return: 随机字符串
    """
    chars = string.ascii_letters + string.digits
    strcode = ''.join(random.sample(chars, randomlength))
    return strcode


def send_register_email(email, send_type='register'):
    """
    发送注册邮件
    :param email: 收件人邮箱
    :param send_type: 发送类型，register表示注册，forget表示找回密码
    :return:
    """
    email_record = EmailVerifyRecord()
    code = random_str()
    email_record.code = code
    email_record.email = email
    email_record.send_type = send_type
    email_record.save()

    if send_type =='register':
        email_title = '注册激活链接'
        email_body = '请点击下面的链接激活你的账号：http://127.0.0.1:8000/active/{0}'.format(code)
    # elif send_type == 'forget':
    #     email_title = '密码重置链接'
    #     email_body = '请点击下面的链接重置你的密码：http://127.0.0.1:8000/reset/{0}'.format(random_str(16))
        send_status = send_mail(email_title, email_body, 'duxjie2021@163.com', [email])
        if send_status:
            pass
